<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>留言板</title>
    <style>
        .container {
            width: 350px;
            height: 300px;
            margin: 0 auto;
            /* border: 1px black solid; */
            text-align: center;
        }

        .grey {
            color: grey;
        }

        .container .row {
            width: 350px;
            height: 40px;

            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .container .row input {
            width: 260px;
            height: 30px;
        }

        #submit {
            width: 350px;
            height: 40px;
            background-color: orange;
            color: white;
            border: none;
            margin: 10px;
            border-radius: 5px;
            font-size: 20px;
        }
    </style>
</head>

<body>
    <div class="container">
        <h1>留言板</h1>
        <p class="grey">输入后点击提交, 会将信息显示下方空白处</p>
        <div class="row">
            <span>谁:</span> <input type="text" name="" id="from">
        </div>
        <div class="row">
            <span>对谁:</span> <input type="text" name="" id="to">
        </div>
        <div class="row">
            <span>说什么:</span> <input type="text" name="" id="say">
        </div>
        <input type="button" value="提交" id="submit" onclick="submit()">
        <!-- <div>A 对 B 说: hello</div> -->
    </div>

    <script src="js/jquery-3.7.1.min.js"></script>
    <script>
        getList();
        function getList() {
            $.ajax({
                type:"get",
                url:"/message/getList",
                success: function(messages) {
                    for(let msg of messages) {
                        let html = "<div>"+msg.from +"对" + msg.to + "说:" + msg.message+"</div>";
                        $(".container").append(html);
                    }
                }
            });
        }
        
        function submit(){
            //1. 获取留言的内容
            var from = $('#from').val();
            var to = $('#to').val();
            var say = $('#say').val();
            if (from== '' || to == '' || say == '') {
                return;
            }
            // 检验完成,发起后端请求
            $.ajax({
                type:"post",
                url:"/message/publish",
                contentType:"application/json", // 后端需要的是json格式的数据,因此需要设置contentType
                data:JSON.stringify({   // data要加上JSON内置方法stringify(),将其转换为json格式的
                    from:from,// 属性名:属性值
                    to:to,
                    message:say
                }),
                success: function(result){
                    if(result.ok==1) {
                        // 添加成功
                        // alert("添加成功");
                        //2. 构造节点
                        let html = "<div>"+from +"对" + to + "说:" + say+"</div>";
                        //3. 把节点添加到页面上    
                        $(".container").append(html);
                        $(":text").val("");
                    } else {
                        // 失败
                        alert("添加失败");
                    }
                }
            });
        }
        
    </script>
</body>

</html>